package com.hwua.client;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

/**
 * 主界面
 */
public class AuctionListFragment extends Fragment {

    private ListView auctionList;
    private Callback mMallback;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.auction_list, container, false);
        auctionList = (ListView)rootView.findViewById(R.id.auction_list);
        //为ListView的列表项添加单事件的监听器
        auctionList.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mMallback.onItemSelected(position,null);//调用回调方法
            }
        });
        return rootView;
    }

    /**
     * 当该Fragment被添加、显示到Activity时，回调该方法
     * @param activity
     */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        //如果Activity没有实现接口Callback，抛出异常
        if(!(activity instanceof Callback)){
            throw  new IllegalStateException("AuctionListFragment所在的Activity必须实现Callback接口");
        }

        //把该Activity当成Callback对象
        mMallback=(Callback)activity;
    }

    //当该Fragment从它所属的Activity中被删除时回调该方法
    @Override
    public void onDetach() {
        super.onDetach();
        //将mMallback赋为null
        mMallback = null;
    }

    /**
     * 选中的项的模式
     * @param activateOnItemClick
     */
    public void setActivateOnItemClick(boolean activateOnItemClick) {
        auctionList.setChoiceMode(activateOnItemClick?ListView.CHOICE_MODE_SINGLE:ListView.CHOICE_MODE_NONE);
    }
}
